home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3219 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  38 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.comet.net!not-for-mail
  3. From: mwilliams1@comet.net
  4. Subject: Re: The size of a file
  5. Message-ID: <11a7cc$142013.177@news.comet.net>
  6. Date: Sat, 27 Jan 1996 01:32:19 GMT
  7. Reply-To: mwilliams1@comet.net
  8. References: <4ebc03$4gv@gate.compart.fi>
  9. X-Newsreader: IBM NewsReader/2 v1.2.5
  10.  
  11. In <4ebc03$4gv@gate.compart.fi>, Fredrik Sandstrom <fred@spider.compart.fi> writes:
  12. >This is driving me nuts.  I can't find a simple way to get the size of a
  13. >disk file.  This must be possible using the standard C library, but I
  14. >haven't figured out how!
  15.  
  16. #include <stdio.h>                                           
  17.                                                              
  18. int main(int argc, char *argv[]) {                           
  19.   FILE *fptr;                                                
  20.   fpos_t length;                                             
  21.   fptr=fopen(argv[1],"r");                                   
  22.   if (fptr==NULL) {                                          
  23.     printf("Argh! Can't open '%s' !\n",argv[1]);             
  24.     exit(-1);                                                
  25.   }                                                          
  26.   fseek(fptr,0,2);                                           
  27.   length=ftell(fptr);                                        
  28.   printf("The size of '%s' is %ld bytes\n",argv[1],length);  
  29.   fclose(fptr);
  30.   return(0);                                                   
  31. }                                                            
  32.  
  33.  
  34.  
  35. -Mike Williams
  36.  
  37.  
  38.